//
//  RSegement.m
//  RSegement
//
//  Created by Zin_戦 on 2018/2/6.
//  Copyright © 2018年 Zin_戦. All rights reserved.
//
#define BADGE_NUMBER_VIEW_TAG                       -10000

#import "RSegement.h"
#import "BadgeNumberLabel.h"
@implementation RSegement

-(void)awakeFromNib{
    
    [super awakeFromNib];
    [_btn0 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    _line0.backgroundColor = [UIColor redColor];
    
}
- (IBAction)selectAction:(UIButton *)sender {
    NSLog(@"%ld",sender.tag);
    [self search_changeColor:sender];
    if ([self.delegate respondsToSelector:@selector(selectIndex:current_btn:)]) {
        
        [self.delegate selectIndex:sender.tag current_btn:sender];
        
    }
}

- (void)search_changeColor :(UIButton *)view{
    
    for (UIView* subView in self.subviews) {

        if (subView.tag ==view.tag) {
            [view setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

            for (UIView *subs in subView.subviews) {
                
                if ([subs isKindOfClass:[UIButton class]]) {
                    [(UIButton *)subs setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                } if ([subs isKindOfClass:[UIImageView class]]) {
                    subs.backgroundColor = [UIColor redColor];
                }
                
            }

        }else{

            for (UIView *subs in subView.subviews) {
                
                if ([subs isKindOfClass:[UIButton class]]) {
                    [(UIButton *)subs setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                } if ([subs isKindOfClass:[UIImageView class]]) {
                    subs.backgroundColor = [UIColor whiteColor];
                }

            }
        }
    }
}


+ (void)setBadgeNumber:(NSInteger)badgeNumber buttonItem:(UIButton *)buttonItem
{
  
    
    UIView *contentView = buttonItem;
    BadgeNumberLabel *badgeNumberLabel= (BadgeNumberLabel *)[contentView viewWithTag:BADGE_NUMBER_VIEW_TAG];
    
    if (badgeNumberLabel == nil) {
        badgeNumberLabel = [[BadgeNumberLabel alloc] init];
        badgeNumberLabel.tag = BADGE_NUMBER_VIEW_TAG;
        [contentView addSubview:badgeNumberLabel];
        [contentView bringSubviewToFront:badgeNumberLabel];
    }
    
    [contentView bringSubviewToFront:badgeNumberLabel];
    badgeNumberLabel.badgeNumber = badgeNumber;
    
    CGFloat offsetX = 0.0;
    if (badgeNumberLabel.bounds.size.width > 15.0) {
        offsetX = 5.0;
    }
    else if (badgeNumberLabel.bounds.size.width > 20) {
        offsetX = 10.0;
    }
   
    badgeNumberLabel.center = CGPointMake(contentView.bounds.size.width - offsetX + badgeNumberLabel.bounds.size.width / 2.0 , badgeNumberLabel.bounds.size.height / 2.0);
    badgeNumberLabel.backgroundColor = [UIColor whiteColor];
}

@end
